package com.dqhl.community.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.dqhl.community.R;
import com.dqhl.community.model.HealthyFile;
import com.dqhl.community.util.Config;
import com.dqhl.community.util.Dlog;
import com.dqhl.community.util.TimeUtils;
import com.dqhl.community.util.ViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cheng on 2016/9/7.
 * 健康档案adapter
 */
public class HealthyFilelistAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater inflater;
    private List<HealthyFile> healthyFileList;
    private OnPictureClickListener onPictureClickListener;

    public void setOnPictureClickListener(OnPictureClickListener onPictureClickListener) {
        this.onPictureClickListener = onPictureClickListener;
    }

    public HealthyFilelistAdapter(Context context, List<HealthyFile> healthyFileList) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.healthyFileList = healthyFileList;
    }

    @Override
    public int getCount() {
        return healthyFileList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = inflater.inflate(R.layout.item_healthy_file, viewGroup, false);
        }
        TextView tvTime = ViewHolder.get(view, R.id.tv_time);
        TextView tvContent = ViewHolder.get(view, R.id.tv_content);
        GridView gvPic = ViewHolder.get(view, R.id.gv_pic);

        HealthyFile healthyFile = healthyFileList.get(i);

        String time = TimeUtils.timeStamp2Date(healthyFile.getCreate_time(), "yyyy-MM-dd");
        tvTime.setText(time);
        tvContent.setText(healthyFile.getInfo());

        final List<String> picList = new ArrayList<>();
        picList.add(healthyFile.getPic_a());
        picList.add(healthyFile.getPic_b());
        picList.add(healthyFile.getPic_c());
        picList.add(healthyFile.getPic_d());
        picList.add(healthyFile.getPic_e());
        picList.add(healthyFile.getPic_f());

        List<String> nullList = new ArrayList<>();
        nullList.add("");
        picList.removeAll(nullList);
        Dlog.e("picList:" + picList);
        gvPic.setAdapter(new PicGridViewAdapter(context, picList));
        gvPic.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Dlog.e("点击了：" + i);
                onPictureClickListener.showBigPicture(Config.Api.base_img_url + picList.get(i));
            }
        });

        return view;
    }

    public interface OnPictureClickListener {
        void showBigPicture(String picPath);
    }

}
